package com.fuershiye.woman.monitor.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuershiye.woman.monitor.model.Permission;
import com.fuershiye.woman.monitor.service.PermissionService;

@Controller
@RequestMapping(value = "/manager/permission")
public class ManagerPermissionController {
    @Autowired
    PermissionService permissionService;

//    @RequiresPermissions("manager:permissionList")
    @RequestMapping(value = "/list.do")
    @ResponseBody
    public List<Permission> list(String name,Model model, HttpServletRequest request) {
        if(StringUtils.isNotBlank(name)) {
        	return permissionService.listByName(name);
        }
        return permissionService.list();
    }

//    @RequiresPermissions("manager:permissionAdd")
    @RequestMapping(value="/add.do")
    @ResponseBody
    public boolean add(String name, String permissionCode, int status, Long id) {
        if(id != null) {
            Permission permission = permissionService.get(id);
            permission.setName(name);
            permission.setPermissionCode(permissionCode);
            permission.setStatus(status);
            permissionService.save(permission);
            return true;
        }
        Permission permission = new Permission();
        permission.setName(name);
        permission.setPermissionCode(permissionCode);
        permission.setStatus(status);
        permissionService.save(permission);
        return true;
    }

//    @RequiresPermissions("manager:permissionDelete")
    @RequestMapping(value="/delete.do")
    @ResponseBody
    public boolean delete(Long id) {
        permissionService.delete(id);
        return true;
    }

}
